package com.xzz.sort;

import java.util.Arrays;

/**
 * @author: hhz
 * @create: 2021-12-29 10:32
 * 冒泡排序
 **/
public class BubbleSort {
    public static void main(String[] args) {
        int[] arr = new int[]{4,6,8,5,9,2,3,7};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }
    public static void bubbleSort(int[] arr){
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-i-1; j++) {
                if(arr[j]>arr[j+1]){
                    swap(arr,j,j+1);
                }
            }
        }
    }
    public static void swap(int[] arr,int i,int j){
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

}
